/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "b2Joint.h"
#include "b2DistanceJoint.h"
#include "b2LineJoint.h"
#include "b2MouseJoint.h"
#include "b2RevoluteJoint.h"
#include "b2PrismaticJoint.h"
#include "b2PulleyJoint.h"
#include "b2GearJoint.h"
#include "b2FixedJoint.h"
#include "../b2Body.h"
#include "../b2World.h"
#include "../../Collision/b2BroadPhase.h"
#include "../../Common/b2BlockAllocator.h"

#include <new>

b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
{
	b2Joint* joint = NULL;
	switch(def->type)
	{
	case e_distanceJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
			joint = new(mem)b2DistanceJoint;
		}
		break;
	case e_mouseJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2MouseJoint));
			joint = new(mem)b2MouseJoint;
		}
		break;
	case e_prismaticJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
			joint = new(mem)b2PrismaticJoint;
		}
		break;
	case e_revoluteJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
			joint = new(mem)b2RevoluteJoint;
		}
		break;
	case e_pulleyJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
			joint = new(mem)b2PulleyJoint;
		}
		break;
	case e_gearJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2GearJoint));
			joint = new(mem)b2GearJoint;
		}
		break;
	case e_lineJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2LineJoint));
			joint = new(mem)b2LineJoint;
		}
		break;
	case e_fixedJoint:
		{
			void* mem = allocator->Allocate(sizeof(b2FixedJoint));
			joint = new(mem)b2FixedJoint;
		}
		break;
	default:
		b2Assert(false);
		break;
	} 

	return joint;
}

void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
{
	joint->~b2Joint();
	switch()
		switch(def->type)
	{
		case e_distanceJoint:
			{
				allocator->Free(sizeof(b2DistanceJoint));
			}
			break;
		case e_mouseJoint:
			{
				allocator->Free(sizeof(b2MouseJoint));
			}
			break;
		case e_prismaticJoint:
			{
				allocator->Free(sizeof(b2PrismaticJoint));
			}
			break;
		case e_revoluteJoint:
			{
				allocator->Free(sizeof(b2RevoluteJoint));
			}
			break;
		case e_pulleyJoint:
			{
				allocator->Free(sizeof(b2PulleyJoint));
			}
			break;
		case e_gearJoint:
			{
				allocator->Free(sizeof(b2GearJoint));
			}
			break;
		case e_lineJoint:
			{
				allocator->Free(sizeof(b2LineJoint));
			}
			break;
		case e_fixedJoint:
			{
				allocator->Free(sizeof(b2FixedJoint));
			}
			break;
		default:
			b2Assert(false);
			break;
	}
}

b2Joint::b2Joint(const b2JointDef* def)
{
	m_type = def->type;
	m_prev = NULL;
	m_next = NULL;
	m_body1 = def->body1;
	m_body2 = def->body2;
	m_collideConnected = def->collideConnected;
	m_islandFlag = false;
	m_userData = def->userData;
}
